
package it.polimi.sartori_daniello_picca.meteocal.gui;

import it.polimi.sartori_daniello_picca.meteocal.boundary.EventManager;
import it.polimi.sartori_daniello_picca.meteocal.boundary.UserManager;
import it.polimi.sartori_daniello_picca.meteocal.entity.Event;
import it.polimi.sartori_daniello_picca.meteocal.entity.Users;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author Ale
 */
@Named
@SessionScoped
public class EventSearchBean implements Serializable{
    
    @EJB
    private EventManager em;
    @EJB
    private UserManager um;
    
    private Event event, selectedevent;
    
    private List<Event> corrispondence;
   
    public EventSearchBean(){
    }
  
     @PostConstruct
     public void init(){
     event = new Event();
     corrispondence = new ArrayList<>();
     }
  
    public String performResearch(){
        corrispondence = em.searchingPublicEvent(getEvent().getEventName(), getEvent().getPlace(), getEvent().getDataEvento());
        return "eventsfound?faces-redirect=true";
    }
      
    public void submitEvent(){
        Users creator = selectedevent.getCreator();
        Users loggedUser = um.getLoggedUser();
        if(creator.getEmail().equals(loggedUser.getEmail())){
            try {
              FacesContext.getCurrentInstance().getExternalContext().redirect("createdEvent.xhtml?e=" +selectedevent.getEventId().toString());
            } catch (IOException ex) {
              Logger.getLogger(CreatorBean.class.getName()).log(Level.SEVERE, null, ex);
           }
        }else{
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("eventInfo.xhtml?e=" +selectedevent.getEventId().toString());
            } catch (IOException ex) {
                Logger.getLogger(CreatorBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
//fare creatorbean
    
    public Event getEvent() {
        if(event == null){
            event = new Event();
        }
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public List<Event> getCorrispondence() {
        return corrispondence;
    }

    public void setCorrispondence(List<Event> corrispondence) {
        this.corrispondence = corrispondence;
    }

    public Event getSelected() {
        return selectedevent;
    }

    public void setSelected(Event selectedevent) {
        this.selectedevent = selectedevent;
    }
    
    
    public Date getToday(){
        return new Date();
    }
       
}